﻿using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using SageKing.Studio.Pages.Template;

namespace SageKing.Studio.Pages.DictType.component;

public class DictDataEditModal(
    IMessageService ShowMessage,
    IConfirmService _confirmService,
    SysDictTypeService dataService,
    SysDictDataService dataDetailService

) : ModalCRUDBase<SysDictData, DictDataEdit, SysDictType, SysDictType>
{
    public PageBase<SysDictType> PageData { get; set; }

    public Action StateHasChanged { get; set; } = () => { };

    public Func<long, Task<List<SysDictData>>> QueryChildListFunc { get; set; }


    public DictTypeEnum isDB { get; set; }

    #region IModalCRUD

    public IModalCRUD<SysDictData, DictDataEdit, SysDictType, SysDictType> MeCrud => this;

    public string TabTitle { get; set; }
    public string ParentName { get; set; }
    public string Title { get; set; } = "新增";
    public string OkTitle { get; set; } = "保存";
    public string CancelText { get; set; } = "取消";
    public bool Visible { get; set; }
    public bool Loading { get; set; }
    public SysDictData Model { get; set; }
    public DictDataEdit Component { get; set; }
    public Error? Error { get; set; }
    public RenderFragment ModalTemplate { get; set; }

    public async Task DeleteData(SysDictData row, SysDictType parent)
    {
        if (row.Id <= 0)
        {
            return;
        }
        string content = $"确认删除【{row.Name}】";
        var confirmResult = await _confirmService.Show(content + "?", "提示", ConfirmButtons.OKCancel);
        if (confirmResult == ConfirmResult.OK)
        {
            await dataDetailService.Delete(row.Id);
            var result = parent.Children.Where(a => a.Id != row.Id).ToList();
            parent.Children = result;
            _ = ShowMessage.SuccessAsync($"删除【{row.Name}】成功", 2);
        }
    }

    public async Task EditOnStatusSwitch(SysDictData entity)
    {
        try
        {
            var id = entity.Id;
            var state = entity.Status;
            string content = !state ? $"确认关闭【{entity.Name}】" : $"确认开启【{entity.Name}】";

            var confirmResult = await _confirmService.Show(content + "?", "提示", ConfirmButtons.OKCancel);
            if (confirmResult == ConfirmResult.OK)
            {
                Expression<Func<SysDictData, bool>> wherefunc = a => a.Id == id;
                Expression<Func<SysDictData, object>> updatefunc = a => new { a.Status };

                await dataDetailService.Update(entity, updatefunc);
                _ = ShowMessage.SuccessAsync($"{content} 成功", 2);
            }
            else
            {
                entity.Status = !state;
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }
    }

    public async Task HandleOk(MouseEventArgs e)
    {
        try
        {
            Loading = true;
            if (Component != null)
            {
                var result = await Component.Me.Save();
                if (!result)
                {
                    Visible = true;
                    Loading = false;
                    return;
                }
            }

            var getParent = PageData.Items.Where(a => a.Id == Model.DictTypeId).FirstOrDefault();

            if (getParent != null)
            {
                getParent.Children = await QueryChildListFunc(Model.DictTypeId);
                dataService.CacheRefresh(getParent.Code);
            }

            _ = ShowMessage.SuccessAsync($"{Title}【{Model.Name}】成功！");
            Visible = false;
            Loading = false;

        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex, $"{Title}【{Model.Name}】失败！");
            // throw ex;
            Loading = false;
        }
    }

    public async Task<bool> InitModel(SysDictType data)
    {
        isDB = data.Type;
        ParentName = data.Name;

        Model = new SysDictData() { DictTypeId = data.Id };

        return await Task.FromResult(true);
    }
    #endregion
}
